home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Demos / OOFILE / Buildable, limited OOFILE / OOFILE partial source / ooflist.cpp < prev    next >
Encoding:
Text File  |  1995-09-26  |  7.0 KB  |  251 lines  |  [TEXT/CWIE]

  1. // Start of bit added by Ken
  2.  
  3. #include "ooflist.hpp"
  4.  
  5. // -------------------------------------------------------
  6. //  O O F I L E _ L i s t E l e m e n t
  7. // -------------------------------------------------------
  8.  
  9. OOF_ListElement::OOF_ListElement(unsigned long theItem)
  10. {
  11.      mItem = theItem;
  12.      mNext = 0;    // assume we'll put it at the end of the list 
  13. }
  14.  
  15. // -------------------------------------------------------
  16. //  O O F I L E _ L i s t
  17. // -------------------------------------------------------
  18.  
  19. //----------------------------------------------------------------------
  20. // OOF_List::OOF_List
  21. //    Initialize a list, empty to start with.
  22. //    Elements can now be added to the list.
  23. //----------------------------------------------------------------------
  24.  
  25. OOF_List::OOF_List()
  26.         mCount = 0;
  27.         mFirst = mLast = 0; 
  28. }
  29.  
  30. //----------------------------------------------------------------------
  31. // OOF_List::~OOF_List
  32. //    prepare a list for deallocation.  If the list still contains any 
  33. //    ListElements, de-allocate them.  However, note that we do *not*
  34. //    de-allocate the "items" on the list -- this module allocates
  35. //    and de-allocates the ListElements to keep track of each item,
  36. //    but a given item may be on multiple lists, so we can't
  37. //    de-allocate them here.
  38. //----------------------------------------------------------------------
  39.  
  40. OOF_List::~OOF_List()
  41.     while (remove() != 0)
  42.     ;     // delete all the list elements
  43. }
  44.  
  45. //----------------------------------------------------------------------
  46. // OOF_List::append
  47. //      append an "item" to the end of the list.
  48. //      
  49. //    Allocate a ListElement to keep track of the item.
  50. //      If the list is empty, then this will be the only element.
  51. //    Otherwise, put it at the end.
  52. //
  53. //    "item" is the thing to put on the list, it can be a pointer to 
  54. //        anything.
  55. //----------------------------------------------------------------------
  56.  
  57. void
  58. OOF_List::append(unsigned long theItem)
  59. {
  60.     OOF_ListElement *element = new OOF_ListElement(theItem);
  61.  
  62.         mCount++;
  63.     if (isEmpty()) {        // list is empty
  64.             mFirst = element;
  65.             mLast = element;
  66.     } else {            // else put it after last
  67.             mLast->mNext = element;
  68.             mLast = element;
  69.     }
  70. }
  71.  
  72. //----------------------------------------------------------------------
  73. // OOF_List::prepend
  74. //      Put an "item" on the front of the list.
  75. //      
  76. //    Allocate a ListElement to keep track of the item.
  77. //      If the list is empty, then this will be the only element.
  78. //    Otherwise, put it at the beginning.
  79. //
  80. //    "item" is the thing to put on the list, it can be a pointer to 
  81. //        anything.
  82. //----------------------------------------------------------------------
  83.  
  84. void
  85. OOF_List::prepend(unsigned long theItem)
  86. {
  87.     OOF_ListElement *element = new OOF_ListElement(theItem);
  88.  
  89.         mCount++;
  90.     if (isEmpty()) {        // list is empty
  91.             mFirst = element;
  92.             mLast = element;
  93.     } else {                        // else put it before first
  94.             element->mNext = mFirst;
  95.             mFirst = element;
  96.     }
  97. }
  98.  
  99. //----------------------------------------------------------------------
  100. // OOF_List::remove
  101. //      remove the first "item" from the front of the list.
  102. // 
  103. // Returns:
  104. //    Removed item, 0 if nothing on the list.
  105. //----------------------------------------------------------------------
  106.  
  107. unsigned long
  108. OOF_List::remove()
  109. {
  110.     OOF_ListElement *element = mFirst;
  111.     unsigned long thing;
  112.  
  113.     if (isEmpty()) 
  114.             return 0;
  115.  
  116.     thing = mFirst->mItem;
  117.     if (mFirst == mLast) {    // list had one item, now has none 
  118.         mFirst = 0;
  119.                 mLast = 0;
  120.     } else {
  121.         mFirst = element->mNext;
  122.     }
  123.         mCount--;
  124.     delete element;
  125.     return thing;
  126. }
  127.  
  128. //----------------------------------------------------------------------
  129. // OOF_List::isEmpty
  130. //      Returns true if the list is empty (has no items).
  131. //----------------------------------------------------------------------
  132.  
  133. bool
  134. OOF_List::isEmpty() const
  135.     if (mFirst == 0)
  136.         return true;
  137.     else
  138.     return false; 
  139. }
  140.  
  141. //----------------------------------------------------------------------
  142. // OOF_List::member
  143. //      Returns true if the list contains theItem.
  144. //----------------------------------------------------------------------
  145.  
  146. bool
  147. OOF_List::member(unsigned long theItem) const
  148.     if (mFirst != 0)
  149.         {
  150.               OOF_ListElement *ptr;        // keep track
  151.  
  152.               for (ptr = mFirst; ptr->mNext != 0; ptr = ptr->mNext)
  153.                 {
  154.                         if(ptr->mItem == theItem)
  155.                             return(true);
  156.                 }
  157.         }
  158.         return false;
  159. }
  160.  
  161. //----------------------------------------------------------------------
  162. // OOF_List::sortedInsert
  163. //      Insert an "item" into a list, so that the list elements are
  164. //    sorted in increasing order.
  165. //      
  166. //    Allocate a ListElement to keep track of the item.
  167. //      If the list is empty, then this will be the only element.
  168. //    Otherwise, walk through the list, one element at a time,
  169. //    to find where the new item should be placed.
  170. //----------------------------------------------------------------------
  171.  
  172. void
  173. OOF_List::sortedInsert(unsigned long theItem)
  174. {
  175.     OOF_ListElement *element = new OOF_ListElement(theItem);
  176.     OOF_ListElement *ptr;        // keep track
  177.  
  178.         mCount++;
  179.     if (isEmpty()) {    // if list is empty, put
  180.         mFirst = element;
  181.         mLast = element;
  182.     } else if (theItem < mFirst->mItem) {    
  183.                 // item goes on front of list
  184.             element->mNext = mFirst;
  185.             mFirst = element;
  186.         } else {        // look for mFirst one in list bigger than item
  187.           for (ptr = mFirst; ptr->mNext != 0; ptr = ptr->mNext) {
  188.               if (theItem < ptr->mNext->mItem) {
  189.                         element->mNext = ptr->mNext;
  190.                       ptr->mNext = element;
  191.                         return;
  192.                 }
  193.             }
  194.             mLast->mNext = element;        // item goes at end of list
  195.             mLast = element;
  196.     }
  197. }
  198.  
  199. //----------------------------------------------------------------------
  200. // List::sortedInsertNoDups
  201. //      Insert an "item" into a list, so that the list elements are
  202. //    sorted in increasing order and there are no duplicates.
  203. //      
  204. //    Allocate a ListElement to keep track of the item.
  205. //      If the list is empty, then this will be the only element.
  206. //    Otherwise, walk through the list, one element at a time,
  207. //    to find where the new item should be placed.
  208. //----------------------------------------------------------------------
  209.  
  210. void
  211. OOF_List::sortedInsertNoDups(unsigned long theItem)
  212. {
  213.     OOF_ListElement *element = new OOF_ListElement(theItem);
  214.     OOF_ListElement *ptr;        // keep track
  215.  
  216.     if (isEmpty()) {    // if list is empty, put
  217.         mFirst = element;
  218.         mLast = element;
  219.         } else if (theItem < mFirst->mItem) {    
  220.             // item goes on front of list
  221.                 element->mNext = mFirst;
  222.                 mFirst = element;
  223.         } else {        // look for first one in list bigger than item
  224.           for (ptr = mFirst; ptr->mNext != 0; ptr = ptr->mNext) {
  225.               if (theItem < ptr->mNext->mItem) {
  226.                         if (theItem == ptr->mItem) {
  227.                             delete element;
  228.                             return;
  229.                         } else {
  230.                             element->mNext = ptr->mNext;
  231.                         ptr->mNext = element;
  232.                             mCount++;
  233.                             return;
  234.                         }
  235.                 }
  236.             }
  237.             mLast->mNext = element;        // item goes at end of list
  238.             mLast = element;
  239.         }
  240. }
  241.  
  242. unsigned long OOF_List::count() const
  243. {
  244.     return mCount;
  245. }
  246. // End of bit added by Ken
  247.